<?php

namespace app\common\model\system;

use think\Model;


/**
 * user_log
 * 登录日志
 * @package app\admin\model\system
 */
class UserIntegralLog extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'create_time';

    /**
     * 用户积分操作记录
     * @param string $error
     * @param string $nickname
     * @param int $login_id
     * @param int $status
     */
    public static function IntergralEdit($user_id = 0, $money = 0, $type = 0, $info = '', $comeid = 0)
    {
        if ($money != 0) {
            $user = (new User())->where('id', $user_id)->find();
            //资产判断
            if ($money < 0 && $user['score'] < abs($money)) {
                throw new \Exception("可用积分不足", 100);
            }
            $update = [
                'id' => $user['id'],
                'score' => $user['score'] + $money,
            ];
            $res = User::update($update);
            $res2 = self::create([
                'user_id' => $user_id,
                'befores' => $user['score'],
                'money' => $money,
                'afters' => $update['score'],
                'type' => $type,
                'info' => $info,
                'comeid' => $comeid
            ]);
            if (!$res || !$res2) {
                throw new \Exception("积分修改失败，请重试", 100);
            }
        }
    }
}
